import axios from 'axios';
import { getParams } from '&helpers/url';
import qs from 'qs'
import {message} from 'antd'

const API = {
    SHOW_ALL_IMG:'/front/showAllBackImg',
    SHOW_ONE_IMG:'/front/showOneBackImg',
    SHOW_NAME_AND_ID:'/front/showNameAndId',
    FSM_CURVE:'/fsm/pairCurve',
    FSM_AVG:'/fsm/averageRate',
    FSM_Get2DAnd3D:'/fsm/2DAnd3D',
    FSM_ALLDATAS:'/fsm/selectFsmData',
    UT_ALLDATAS:'/ut/showAllLine',
    DELETE_DEVICE: '/device/delete',
};

export const models = {
    name: 'equipmentPage',
    model: {
        state: {
            idList:[],
            backImgList:[],
            ImgData:{},
            curveTime:{},
            ltOptionData:{}, //折线图
            rtOptionData:{},
            svOptionData:{},
            ltCurveData:[], //3d曲面图 
            rtCurveData:[],
            svCurveData:[],
            fcCurveData:[],
            UtData:[],
            Utdescribe:'',
            xxx:'',
            averageRateData:{}, //平均腐蚀速率
            choice:'all',
            width:550,
            height:300,
            equip:{},
            newEquipPosition:{
              x:116.423736,
              y:39.916458,
            },
            Utequip:{
              equipId:6,
              positionId:72290272,
              des:'yosemite'
            },
            allFsmDatas:[],//查询fsm设备信息
            utDatas:[]
        },
        reducers: {
            setState(state, payLoad) {
                return {
                    ...state,
                    ...payLoad
                }
            }
        },
        effects: dispatch => {
            return {
                async getIdList() {
                    const { code, data } = await axios.post(API.SHOW_NAME_AND_ID);
                    code === 0 && dispatch.equipmentPage.setState({idList: data})
                },
                async getBackImg() {
                  const { code , data } = await axios.post(API.SHOW_ALL_IMG);
                  code === 0 && dispatch.equipmentPage.setState({backImgList: data})
                },
                async showOneBackImg() {
                  const idList = await axios.post(API.SHOW_NAME_AND_ID);
                  const BackImgId = idList.data[0].id;
                  var paramsList =  getParams({BackImgId})
                  const { code , data, msg } = await axios.post(API.SHOW_ONE_IMG+'/'+paramsList,BackImgId);
                  code === 0 && dispatch.equipmentPage.setState({ImgData:data})
                  code !== 0 && message.error(msg)
                },
                async getCurve(equip) {
                  // const idList = await axios.post(API.SHOW_NAME_AND_ID);
                  // const BackImgId = idList.data[0].id;
                  // var paramsList =  getParams({BackImgId})
                  // const  ImgData  = await axios.post(API.SHOW_ONE_IMG,qs.stringify(BackImgId));
                  // const equip = ImgData.data.backImgVo2s[0];
                  // console.log(equip);
                  const formData = {
                    equipId: equip.equipId,
                    positionId: equip.positionId,
                    type:equip.allType?equip.allType:'allType',
                    choice:equip.choice,
                    startTime: equip.startTime ? equip.startTime : 'allTime',
                    endTime: equip.endTime ? equip.endTime : 'allTime'
                  }
                  const {code, data, msg} = await axios.post(API.FSM_CURVE,qs.stringify(formData));
                  switch (equip.choice) {
                    case 0:
                      code === 0 && dispatch.equipmentPage.setState({ltOptionData: data, rtOptionData: data, svOptionData: data});
                      code !== 0 && dispatch.equipmentPage.setState({ltOptionData: [], rtOptionData: [], svOptionData: []}) && message.error(msg)
                      break;
                    case 1:
                      code === 0 && dispatch.equipmentPage.setState({ltOptionData: data})
                      code !== 0 && dispatch.equipmentPage.setState({ltOptionData:[]}) && message.error(msg)
                      break;
                    case 2:
                      code === 0 && dispatch.equipmentPage.setState({rtOptionData: data})
                      code !== 0 && dispatch.equipmentPage.setState({rtOptionData:[]}) && message.error(msg)
                      break;
                    case 3:
                      code === 0 && dispatch.equipmentPage.setState({svOptionData: data})
                      code !== 0 && dispatch.equipmentPage.setState({svOptionData:[]}) && message.error(msg)
                      break;
                    default:
                      break;
                  }
                },
                async getAverageRate(equip){
                  const formData = {
                    equipId: equip.equipId,
                    positionId: equip.positionId,
                    startTime: equip.startTime ? equip.startTime : 'allTime',
                    endTime: equip.endTime ? equip.endTime : 'allTime'
                  }
                  const {code, data} = await axios.post(API.FSM_AVG,qs.stringify(formData));
                  code === 0 && dispatch.equipmentPage.setState({averageRateData:data});
                },
                async getAllFsmDatas(equip){//表格展示 查询fsm信息
                  const formData = {
                    // equipId: equip.equipId,
                    equipId:'00000000000015',
                    time:'newTime',//2016-10-25
                  }
                  const {code, data} = await axios.post(API.FSM_ALLDATAS,qs.stringify(formData));
                  code === 0 && dispatch.equipmentPage.setState({allFsmDatas:data});
                },
                async getAllUtDatas(equip){//折线图展示 查询Ut设备信息
                  const formData = {
                    equipId:equip.equipId,
                    positionId: equip.positionId
                  }
                  console.log(formData)
                  const {code, data} = await axios.post(API.UT_ALLDATAS,qs.stringify(formData));
                  code === 0 && dispatch.equipmentPage.setState({utDatas:data});
                },
                async get2DAnd3D(equip){
                  const formData = {
                    equipId: equip.equipId,
                    type: equip.type,
                    time: 'newTime',
                    choice: equip.choice
                  };
                  console.log(equip);
                  const {code, data, msg} = await axios.post(API.FSM_Get2DAnd3D,qs.stringify(formData));
                  if (equip.type === 1) {
                    code === 0 && dispatch.equipmentPage.setState({ltCurveData:data});
                    code !== 0 && dispatch.equipmentPage.setState({ltCurveData:data}) 
                  }else if(equip.type === 2){
                    code === 0 && dispatch.equipmentPage.setState({rtCurveData:data});
                    code !== 0 && dispatch.equipmentPage.setState({rtCurveData:data})
                  }else if(equip.type === 3){
                    code === 0 && dispatch.equipmentPage.setState({svCurveData:data});
                    code !== 0 && dispatch.equipmentPage.setState({svCurveData:data}) 
                  }else {
                    code === 0 && dispatch.equipmentPage.setState({fcCurveData:data});
                    code !== 0 && dispatch.equipmentPage.setState({fcCurveData:data}) 
                  }
                  
                },
                async deleteDevice(Id){
                  const {code, data, msg} = await axios.post(API.DELETE_DEVICE,qs.stringify(Id));
                  code === 0 && message.success('删除成功') && await getBackImg();
                  code !== 0 && message.error(msg)
                }
            }
        }
    }
};
